import { Injectable } from '@angular/core';

/**
 * This service is intend to share the data between different components.
 */
@Injectable()
export class DataSharingService {
    // This property would be object format.
    // Any component can put data with its unique key. And its value can be a nested object.
    // For example:
    // { sidebarTree: { selectedTableTypes: [...] } }
    // This example shows sidebar-tree component puts the data with key 'sidebarTree'.
    // The value is an object with one key 'selectedTableTypes' and its value is an array.
    private sharedDataSource: any = {};

    constructor() { }

    /**
     * Any component can put data with its unique key. And its value can be a nested object.
     * For example:
     * { sidebarTree: { selectedTableTypes: [...] } }
     * This example shows sidebar-tree component puts the data with key 'sidebarTree'.
     * The value is an object with one key 'selectedTableTypes' and its value is an array.
     * @param key The string of the data key. It's usally would be the data setter's name.
     * @param data The Data value of any format.
     */
    setValue(key: string, data: any): void {
        this.sharedDataSource[key] = data;
    }

    /**
     * Gets the data with the key.
     * @param key The string of the key.
     * @returns The any format of the data.
     */
    getValue(key: string): any {
        return this.sharedDataSource[key];
    }
}